package org.tio.sitexxx.service.service.qj;

import com.alibaba.fastjson.JSON;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.sitexxx.service.model.main.Coupon;
import org.tio.sitexxx.service.vo.Const;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CouponService {
    private static Logger log	= LoggerFactory.getLogger(CouponService.class);
    public static final CouponService me	= new CouponService();
    final Coupon dao	= new Coupon().dao();
    /**
     * 保存数据
     * @param demo
     * @return
     */
    public boolean save(Coupon demo) {
        return demo.save();
    }

    /**
     *
     * @param query
     * @return
     */
    public Integer Count(Map<String,Object> query){
        String sqlid = "coupon.Count";
        String db = Const.Db.TIO_SITE_MAIN;
        Kv cond=Kv.create().set(query);
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid,cond);
        int count = Db.use(db).queryInt(sqlPara.getSql(),sqlPara.getPara());
        return count;
    }

    /**
     *
     * @param query
     * @return
     */
    public List<Coupon> getCoupons(Map<String,Object> query){
        List<Coupon>resultLsit=new ArrayList<>(8);
        String sqlid = "coupon.getCoupons";
        String db = Const.Db.TIO_SITE_MAIN;
        Kv cond=Kv.create().set(query);
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid,cond);
        List<Record>list  = Db.use(db).find(sqlPara);
        for(Record record:list){
            resultLsit.add(JSON.parseObject(JSON.toJSONString(record.getColumns()),Coupon.class));

        }
        return resultLsit;

    }

    /**
     * update   用过作废
     * @param Coupon
     * @return
     */
    public boolean update(Coupon Coupon){
        return Coupon.update();
    }

    /**
     *
     * @param id
     * @return
     */
    public Coupon getCouponById(int id){
        List<Coupon>resultLsit=new ArrayList<>(8);
        String sqlid = "coupon.getCouponById";
        String db = Const.Db.TIO_SITE_MAIN;
        Kv cond=Kv.by("id",id);
        System.out.println("id:"+id);
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid,cond);
        Record record = Db.use(db).findFirst(sqlPara);
        Coupon coupon=JSON.parseObject(JSON.toJSONString(record.getColumns()),Coupon.class);
        return coupon;

    }




}
